package com.example.pixelyear.utils.extension

import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.os.Build
import android.view.View
import android.view.View.*
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.pixelyear.Interface.onDoubleClickListener

/**
 * Package    : com.zjedu.xueyuan.utils
 * ClassName  : ViewUtils
 * Description: $View扩展方法
 * Date       : 2019/5/17 9:03
 */

/**
 * 防止双击
 */
fun View.setOnDoubleClickListener(click: (view: View?) -> Unit) {
    setOnClickListener(object : onDoubleClickListener() {
        override fun onDoubleClick(view: View?) {
            click.invoke(view)
        }
    })
}

/**
 * 设置ImageView亮度
 */
fun ImageView.setLight(brightness: Float) {
    val cMatrix = ColorMatrix()
    cMatrix.set(floatArrayOf(1f, 0f, 0f, 0f, brightness, 0f, 1f, 0f, 0f, brightness, // 改变亮度
        0f, 0f, 1f, 0f, brightness, 0f, 0f, 0f, 1f, 0f))
    colorFilter = ColorMatrixColorFilter(cMatrix)
}

fun TextView.setStyle(id: Int) {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        setTextAppearance(id)
    } else
        setTextAppearance(context, id)
}

fun EditText.setNotCanEdit() {
    isEnabled = false
    isClickable = false
}

//设置隐藏
fun View.gone() {
    if (visibility == VISIBLE || visibility == INVISIBLE)
        visibility = GONE
}

fun View.visi() {
    if (visibility == GONE || visibility == INVISIBLE)
        visibility = VISIBLE
}

fun View.isVisi(): Boolean {
    return visibility == VISIBLE
}

fun View.invisi() {
    if (visibility == GONE || visibility == VISIBLE)
        visibility = INVISIBLE
}

fun View.setVisi(b: Boolean) {
    if (b)
        visi()
    else gone()
}

fun View.setMargin(left: Int, top: Int, right: Int, bottom: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).setMargins(left, top, right, bottom)
    requestLayout()
}

fun View.setRelativeLayoutMargins(left: Int, top: Int, right: Int, bottom: Int) {
    (layoutParams as RelativeLayout.LayoutParams).setMargins(left, top, right, bottom)
    requestLayout()
}

fun View.setRelativeLayoutPadding(left: Int, top: Int, right: Int, bottom: Int) {
    setPadding(left, top, right, bottom)
    requestLayout()
}


fun View.setLinearLayoutMargin(left: Int, top: Int, right: Int, bottom: Int) {
    (layoutParams as LinearLayout.LayoutParams).setMargins(left, top, right, bottom)
    requestLayout()
}

fun EditText.stringText(): String {
    return text.toString()
}

fun EditText.yxsText(t: String) {
    setText(t)
}

//垂直列表
fun RecyclerView.verticalManager() {
    layoutManager = LinearLayoutManager(context)
}

//垂直列表
fun RecyclerView.verticalManager(success: (manager: LinearLayoutManager) -> Unit) {
    layoutManager = LinearLayoutManager(context).apply {
        success.invoke(this)
    }
}

//水平列表
fun RecyclerView.horizontalManager() {
    layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
}

//网格列表
fun RecyclerView.gridManager(spanCount: Int) {
    layoutManager = GridLayoutManager(context, spanCount)
}

//自适应列表
fun RecyclerView.staggeredGridLayoutLayoutManager(spanCount: Int, oriental: Int) {
    layoutManager = StaggeredGridLayoutManager(spanCount, oriental)
}




fun View.setBGColor(color: String?) {
    try {
        if (!color.isNullOrEmpty())
            setBackgroundColor(Color.parseColor(color))
    } catch (e: NumberFormatException) {
    }
}



fun TextView.setColor(color: String?) {
    try {
        if (color != null && color.isNotEmpty())
            setTextColor(Color.parseColor(color))
    } catch (e: NumberFormatException) {
    }
}


/**
 * 返回一个View上面的图像
 * 必须在调用后销毁缓存(只要保证Bitmap不再使用才可以销毁)
 */
fun View.getImage(): Bitmap? {
    isDrawingCacheEnabled = true
    measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
//          FrameLayout.layout(0, 0, FrameLayout.measuredWidth, FrameLayout.measuredHeight) 如果加上此句会使FrameLayout重新绘制，水印图片跑到左上角
    val bitmap = Bitmap.createBitmap(drawingCache)
    destroyDrawingCache()
    return bitmap
}
